package com.example.youjingjing.electronicbalance.widge;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.youjingjing.electronicbalance.R;
import com.example.youjingjing.electronicbalance.presenter.LoginPresenter;
import com.example.youjingjing.electronicbalance.util.MyToast;
import com.example.youjingjing.electronicbalance.view.LoginView;

import butterknife.BindView;
import butterknife.ButterKnife;



public class LoginActivity extends BaseActivity<LoginView,LoginPresenter> implements LoginView,View.OnClickListener{
    @BindView(R.id.login_edit_name)
    EditText etUserName;
    @BindView(R.id.login_edit_psw)
    EditText etPassword;

    @BindView(R.id.login_btn_commit)
    Button btn_commit;

    @BindView(R.id.login_textInputLayout_name)
    TextInputLayout textInputLayout_name;

    @BindView(R.id.login_textInputLayout_key)
    TextInputLayout textInputLayout_key;

    @BindView(R.id.login_check_remember)
    CheckBox checkBox_remember;

    @BindView(R.id.login_check_autoLogin)
    CheckBox checkBox_autoLogin;

    private LoginPresenter loginPresenter;

    String name;
    String psw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginPresenter = createPresenter();
        //设置可以计数
        textInputLayout_key.setCounterEnabled(true);
        //计数的最大值
        textInputLayout_key.setCounterMaxLength(6);


        btn_commit.setOnClickListener(this);
        checkBox_remember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(checkBox_remember.isChecked()){
                    MyToast.show(LoginActivity.this,"记住密码");
                    writeBoolean("ISCHECK", true);
                }else{
                    MyToast.show(LoginActivity.this,"不记住密码");
                    writeBoolean("ISCHECK", false);
                }
            }
        });

        rememberKey();


        checkBox_autoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(checkBox_autoLogin.isChecked()){
                    MyToast.show(LoginActivity.this,"自动登录");
                    writeBoolean("AUTO_ISCHECK", true);

                    checkBox_remember.setChecked(true);
                    writeBoolean("ISCHECK", true);

                }else{
                    MyToast.show(LoginActivity.this,"不自动登录");
                    writeBoolean("AUTO_ISCHECK", false);
                }
            }
        });
        autoLogin();
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkPswd(etPassword.getText().toString(),true);
            }
        });
        etUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                checkName(etUserName.getText().toString(),true);
            }
        });
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(); //具体界面对应的presenter由具体界面决定
    }

    @Override
    public void showLoginSuccess() {
        Toast.makeText(this,"登录成功..", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoginFailure(String msg) {
        jumpActivity(RegisterActivity.class);
        MyToast.show(this,"可以注册");
    }

    @Override
    public void showLogging() {
        Toast.makeText(this,"正在登录..", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showNetWorkError() {
        Toast.makeText(this,"网络故障..", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void rememberKey() {
        name = readData("name");
        if (readBoolean("ISCHECK")){
            checkBox_remember.setChecked(true);

            psw = readData("psw");
            etPassword.setText(psw);
            etUserName.setText(name);
        }else{
            checkBox_remember.setChecked(false);
            etPassword.setText("");
            etUserName.setText(name);
        }
    }

    @Override
    public void autoLogin() {
        if(readBoolean("AUTO_ISCHECK")){
            checkBox_autoLogin.setChecked(true);
            rememberKey();
        }else{
            checkBox_autoLogin.setChecked(false);
        }
    }

    @Override
    public void setStatus(boolean boo) {

    }

    /**
     *若用户名为空且用户是想登录，提示错误，返回false。不为空，错误设为null
     */
    private boolean checkName(CharSequence name,boolean isLogin) {
        if(TextUtils.isEmpty(name)) {
            if(isLogin) {
                textInputLayout_name.setError("用户名不能为空");
                return false;
            }
        }else{
            textInputLayout_name.setError(null);
        }
        return true;
    }
    private boolean checkPswd(CharSequence pswd,boolean isLogin) {
        if(TextUtils.isEmpty(pswd)) {
            if(isLogin) {
                textInputLayout_key.setError("密码不能为空");
                return false;
            }
        }else{
            textInputLayout_key.setError(null);
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.login_btn_commit:
                if(!checkName(etUserName.getText(),true))
                    return;
                if(!checkPswd(etPassword.getText(),true))
                    return;
                name = etUserName.getText().toString();
                psw = etPassword.getText().toString();
                writeString("name",name);
                writeString("psw",psw);
                presenter.login(LoginActivity.this, etUserName.getText().toString(), etPassword.getText().toString());
                break;
            case 1:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        jumpActivity(MainActivity.class);
    }
}
